//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing.Diagram
{
    internal class StyleDefinition : IOfficeXmlSerializable
    {
        internal const string xmlName = "styleDef";

        private List<SDDescription> _descriptions = new List<SDDescription>();
        private List<SDName> _titles = new List<SDName>();
        private List<SDCategory> catLst = new List<SDCategory>();
        private OfficeArtExtensionList extLst;
        private string minVer;
        private Scene3D scene3d;
        private List<StyleLabel> styleLbl;
        private string uniqueId;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                uniqueId = c.GetString("uniqueId");
                minVer = c.GetOptionalString("minVer", "http://schemas.openxmlformats.org/drawingml/2006/diagram");

                _titles.Clear();
                _titles.AddRange(c.ReadOptionalWhile<SDName>("title"));
                _descriptions.Clear();
                _descriptions.AddRange(c.ReadOptionalWhile<SDDescription>(SDDescription.xmlName));
                catLst.Clear();
                catLst.AddRange(c.ReadElements<SDCategory>("catLst", "cat"));
                scene3d = c.ReadOptionalElement<Scene3D>("scene3d");
                styleLbl = new List<StyleLabel>();
                styleLbl.Clear();
                styleLbl.AddRange(c.ReadOptionalWhile<StyleLabel>("styleLbl"));
                extLst = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.DeclareNamespace(DrawingConstants.DrawingNamespace);
                c.SetString("uniqueId", uniqueId );
                c.SetOptionalString("minVer", minVer ,"http://schemas.openxmlformats.org/drawingml/2006/diagram");
                c.WriteElements(_titles);
                c.WriteElements(_descriptions);
                if (catLst.Count > 0)
                {
                    using (var c2 = c.Write("catLst"))
                    {
                        c2.WriteElements(catLst);
                    }
                }
                c.WriteOptionalElement(scene3d );
                c.WriteElements(styleLbl);
                c.WriteOptionalElement(extLst );
            }
        }

        #endregion
    }
}